package com.cskaoyan.javase.swing.manager.stage5.model;


import java.io.*;

/**
 * 模拟学生数据源
 *
 * @since 14:09
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class StudentData {
    public static final Student[] STUDS = new Student[20];
    public static final String[] COLUMNS;
    private static int count;
    public static File studentFile = new File("students.txt");
    static {
        // 初始化 读取文件
        init();
        // 表格列数据
        COLUMNS = new String[]{"学号", "姓名", "性别", "学校", "专业", "年龄", "城市", "手机号", "电子邮箱"};
    }

    /*
     * 初始化操作 读取文件 按行读取,一行就是一个学生对象 , 再还原回学生对象
     * 创建日期：2021/12/27 21:04
     * @return void
     * @author 景天
     */
    private static void init() {
        try {
            BufferedReader in = new BufferedReader(new FileReader(studentFile));
            String line;
            while ((line = in.readLine()) != null) {
                String[] studentInfo = line.split(",");
                String id = studentInfo[0].split("=")[1];
                String name = studentInfo[1].split("=")[1];
                String gender = studentInfo[2].split("=")[1];
                String school;
                String[] schoolInfo = studentInfo[3].split("=");
                if (schoolInfo.length == 1) {
                    school = "";
                }else {
                    school = schoolInfo[1];
                }
                String major;
                String[] majorInfo = studentInfo[4].split("=");
                if (majorInfo.length == 1) {
                    major = "";
                } else {
                    major = majorInfo[1];
                }
                String age = studentInfo[5].split("=")[1];
                String city = studentInfo[6].split("=")[1];
                String phone = studentInfo[7].split("=")[1];
                String email = studentInfo[8].split("=")[1];
                Student student = new Student();
                student.setStuId(id);
                student.setName(name);
                student.setGender(gender);
                student.setSchool(school);
                student.setMajor(major);
                student.setAge(age);
                student.setCity(city);
                student.setPhone(phone);
                student.setEmail(email);
                STUDS[count] = student;
                count++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
